package com.easepal802s.project.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.easepal7506a.project.ui.activity.BaseActivity;
import com.easepal802s.project.R;


/**
 * 网页界面
 * Created by HourGlassRemember on 2018/12/24.
 */
public class WebViewActivity extends BaseActivity {

    private static final String TAG = "WebViewActivity";

    private WebView webView;


    /**
     * 设置标题栏以下布局 ID
     *
     * @return 标题栏以下布局 ID
     */
    @Override
    public int setLayoutId() {
        return R.layout.activity_web_view;
    }

    @Override
    public void initTitleBar() {
        super.initTitleBar();
        showTitleBar(true);
        titleBar.setTitle(getResources().getString(R.string.yhxy),"#000000");
        titleBar.showPowerView(false);
    }

    /**
     * 初始化视图
     *
     * @param layoutId 设置标题栏以下布局 ID
     */
    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        webView = findViewById(R.id.web_view);
        webView.setWebViewClient(webViewClient);// 设置 WebViewClient
        webView.setWebChromeClient(webChromeClient);// 设置 WebChromeClient
        WebSettings webSettings = webView.getSettings();
        if (null == webSettings) {
            return;
        }
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);// 设置 WebView 的缓存模式：不使用缓存，只从网络获取数据
        webSettings.setSupportZoom(true);// 支持缩放，默认为 true，是下面两个的前提
        webSettings.setBuiltInZoomControls(true);// 设置内置的缩放控件，若为 false 表示该 WebView 不可缩放
        webSettings.setDisplayZoomControls(false);// 隐藏原生的缩放按钮
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSaveFormData(true);
        webSettings.setJavaScriptEnabled(true);

        String url = "http://47.96.169.127:8020/pages/agreement/index.html";

        if (!TextUtils.isEmpty(url)) {
            Log.i(TAG, "initView --- url = " + url);
            webView.loadUrl(url);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();// 释放资源
        webView = null;
    }

    // WebViewClient：主要帮助 WebView 处理各种通知、请求事件
    private WebViewClient webViewClient = new WebViewClient() {
        /**
         * 页面开始加载，一次 Frame 加载对应一次回调
         *
         * @param view
         * @param url
         * @param favicon
         */
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        /**
         * 页面请求完成，一次 Frame 加载对应一次回调
         *
         * @param view
         * @param url
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.i(TAG, "onPageFinished --- url = " + url);
        }

        /**
         * 拦截 URL
         *
         * @param view
         * @param request
         * @return
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            Log.i(TAG, "shouldOverrideUrlLoading111 --- request = " + request);
            return super.shouldOverrideUrlLoading(view, request);
        }

        /**
         * 是否在 WebView 内加载页面
         *
         * @param view
         * @param url
         * @return
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            Log.i(TAG, "shouldOverrideUrlLoading222 --- url = " + url);
            return true;
        }

        /**
         * WebView 访问 URL 出错时回调
         * 例如访问网页时报错404，在这个方法回调的时候可以加载错误页面
         *
         * @param view
         * @param request
         * @param error
         */
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            Log.i(TAG, "onReceivedError --- request = " + request);
            Log.i(TAG, "onReceivedError --- error = " + error);
        }
    };

    // WebChromeClient：主要辅助 WebView 处理 Javascript 的对话框、网站图标、网站 title、加载进度等
    private WebChromeClient webChromeClient = new WebChromeClient() {
        /**
         * 处理 JS 中的 Prompt 对话框
         *
         * @param view
         * @param url
         * @param message
         * @param defaultValue
         * @param result
         * @return
         */
        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
            return super.onJsPrompt(view, url, message, defaultValue, result);
        }

        /**
         * 处理 JS 中调用 alert() 方法产生的对话框
         * 不支持 JS 的 Alert 弹窗，需要自己监听然后通过 dialog 弹窗
         *
         * @param view
         * @param url
         * @param message
         * @param result
         * @return
         */
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            Log.i(TAG, "onJsAlert --- url = " + url);
            Log.i(TAG, "onJsAlert --- message = " + message);
            Log.i(TAG, "onJsAlert --- result = " + result);
            return super.onJsAlert(view, url, message, result);
        }

        /**
         * 处理 JS 中的 Confirm 对话框
         *
         * @param view
         * @param url
         * @param message
         * @param result
         * @return
         */
        @Override
        public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
            return super.onJsConfirm(view, url, message, result);
        }

        /**
         * 获取网页标题
         *
         * @param view
         * @param title
         */
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            Log.i(TAG, "onReceivedTitle --- title = " + title);
        }

        /**
         * 获取网页 icon
         *
         * @param view
         * @param icon
         */
        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {
            super.onReceivedIcon(view, icon);
            Log.i(TAG, "onReceivedIcon --- icon = " + icon);
        }

        /**
         * 加载进度回调
         *
         * @param view
         * @param newProgress
         */
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            Log.i(TAG, "onProgressChanged --- newProgress = " + newProgress);
        }
    };

}
